package com.chagee.store.operation.server.service;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.JSONUtil;
import com.chagee.personnel.api.event.response.ListByStoreNoListResp;
import com.chagee.store.operation.api.request.StorePersonnelRequest;
import com.chagee.store.operation.api.response.StorePersonnelResponse;
import com.chagee.store.operation.common.exception.BizErrorCode;
import com.chagee.store.operation.common.exception.BizException;
import com.chagee.store.operation.server.feign.vo.User;
import com.chagee.store.operation.server.feign.vo.UserPostVO;
import com.chagee.store.operation.server.service.third.BladeUserService;
import com.personnel.auth.service.PersonnelAuthService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author : hk
 * @Date : 2024/7/31
 */

@Service
@Slf4j
@RequiredArgsConstructor
public class StorePersonnelBizService {

	@Resource
	private PersonnelAuthService personnelAuthService;

	@Resource
	private PersonnelThirdService personnelThirdService;

	@Resource
	private BladeUserService bladeUserService;


	public List<StorePersonnelResponse> userList(StorePersonnelRequest request) {

		List<String> storeNos = CollectionUtil.newArrayList(request.getStoreNo());

		List<ListByStoreNoListResp> personnelList = personnelThirdService.listByStoreNoList(storeNos);
		if (CollectionUtil.isEmpty(personnelList)) {
			return new ArrayList<>();
		}
		List<StorePersonnelResponse> userList = personnelList.stream().map(s -> {
			StorePersonnelResponse personnelResponse = new StorePersonnelResponse();
			personnelResponse.setId(s.getId());
			personnelResponse.setRealName(s.getRealName());
			personnelResponse.setPhone(s.getPhone());
			String phoneEncipher = s.getPhone().replaceAll("(\\d{3})\\d{4}(\\d{4})","$1****$2");
			personnelResponse.setNameAndPhone(s.getRealName().concat("(").concat(phoneEncipher).concat(")").concat(StringUtils.isNotBlank(s.getPostName()) ? s.getPostName() : StringUtils.EMPTY));
			return personnelResponse;
		}).collect(Collectors.toList());

		return userList;
	}

	public User userInfoByPhone(String phone, String nameAndPhone) {
		User userVO = bladeUserService.userInfoByPhone(phone);

		if (Objects.isNull(userVO) || Objects.isNull(userVO.getId())) {
			throw new BizException(BizErrorCode.QUERY_USER_DETAIL_FAIL.getErrorCode(), nameAndPhone.concat("在霸王功夫账号不存在或异常 请联系管理员"));
		}
		return userVO;
	}

	public Boolean addUserRole(Collection<?> userIds, String roleName) {

		List<Long> userList = userIds.stream().map(t -> {
			if (t instanceof String) {
				if (StringUtils.isBlank((String) t)) {
					return null;
				}
				return Long.valueOf((String) t);
			} else {
				if (Objects.isNull(t)) {
					return null;
				}
				return (Long) t;
			}
		}).filter(Objects::nonNull).distinct().collect(Collectors.toList());
		if (CollectionUtils.isEmpty(userList)) {
			return false;
		}
		for (Long userId : userList) {
			try {
				bladeUserService.addUserRole(userId, roleName);
			} catch (Exception e) {
				log.error("配置用户权限失败 userId{} roleName {}", userId, roleName);
			}
		}
		return true;
	}


}
